package jp.sourceforge.akjrcp.ui.param;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import jp.sourceforge.akjrcp.ui.param.AbstractParamCompositeItem.SelectionCheck;

import org.apache.commons.lang.ArrayUtils;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;

public class ParamCheckGroup extends AbstractParamCompositeItem{
private String key;
private String label;
private String[] params;
private String value="";//ffmpeg style +bitexact+loop 
List<Button> buttons=new ArrayList<Button>();
private Group group;
public String getKey() {
	return key;
}

public String getLabel() {
	return label;
}

public void setToolTipText(String text){
	group.setToolTipText(text);
	for (int i = 0; i < buttons.size(); i++) {
		buttons.get(i).setToolTipText(text);
	}
}

public String[] getParams() {
	return params;
}
public String[] getSelectionValues(){
	List<String> arrayList=new ArrayList<String>();
	for (int i = 0; i < buttons.size(); i++) {
		Button bt=buttons.get(i);
		if(bt.getSelection()){
			arrayList.add(bt.getText());
		}
	}
	return (String[]) arrayList.toArray(new String[arrayList.size()]);
}
public String getValue() {
	Map<String,String> map=new HashMap<String,String>();
	String vs[]=value.split("\\+");
	for (int i = 0; i < vs.length; i++) {
		if(vs[i].length()>0){
		map.put(vs[i], "");
		}
	}
	String ret="";
	for (int i = 0; i < buttons.size(); i++) {
		Button bt=buttons.get(i);
		if(bt.getSelection()){
			ret+="+"+bt.getText();
		}
		map.remove(bt.getText());
	}
	//keep default values.
	Iterator iter=map.keySet().iterator();
	while(iter.hasNext()){
		String key=(String) iter.next();
		ret+="+"+key;
	}
	return ret;
}

/*
 * only this selected value.
 */
public String getThisValue(){
	String ret="";
	for (int i = 0; i < buttons.size(); i++) {
		Button bt=buttons.get(i);
		if(bt.getSelection()){
			ret+="+"+bt.getText();
		}
	}
	return ret;
}

public void setValue(String value) {
	String vs[]=value.split("\\+");
	for (int i = 0; i < buttons.size(); i++) {
		Button bt=buttons.get(i);
		boolean find=false;
		for (int j = 0; j < vs.length; j++) {
			if(bt.getText().equals(vs[j])){
				find=true;
				break;
			}
		}
		bt.setSelection(find);
	}
	this.value = value;
}
	public ParamCheckGroup(Composite parent, int style,String key,String label,String[] params,int grid) {
		super(parent, style);
		this.key=key;
		this.label=label;
		this.params=params;
		this.setLayout(new FillLayout());
		group = new Group(this,SWT.NONE);
		group.setText(label);
		group.setLayout(new GridLayout(grid,false));
		SelectionCheck ch=new SelectionCheck();
		for (int i = 0; i < params.length; i++) {
			Button check=new Button(group,SWT.CHECK);
			check.addSelectionListener(ch);
			check.setText(params[i]);
			buttons.add(check);
		}
	}

	private String sortAndRemoveEmpty(String keys[]){
		String ret="";
		
		Arrays.sort(keys);
		for (int i = 0; i < keys.length; i++) {
			if(!keys[i].equals("")){
				ret+="+"+keys[i];
			}
		}
		return ret;
	}
	
	public boolean isModified() {
		String values[]=value.split("\\+");
		String oldValue=sortAndRemoveEmpty(values);
		
		String newvalues[]=getValue().split("\\+");
		String newValue=sortAndRemoveEmpty(newvalues);
		
		return !oldValue.equals(newValue);
	}

}
